<template>
  <el-container class="home-container">
    <el-header>
      <div class="header-left">
        <img src="../assets/logo.png" />
        <h2>电商后台管理系统</h2>
      </div>
      <el-button type="info" @click='logout'>退出</el-button></el-header>
    <el-container>
      <el-aside :width="isOpen ? '64px' : '200px'">
        <div class="openAndClose" @click="openAndClose">|||</div>
        <el-menu background-color="#333744" text-color="#fff" active-text-color="#409eff" :unique-opened="true" :collapse="isOpen" :collapse-transition="false" :router="true" :default-active="activePath" >
          <el-submenu :index="item.id+''" :key="item.id" v-for="item in menuList">
            <template slot="title">
              <i :class="iconList[item.id]" class="icon"></i>
              <span>{{item.authName}}</span>
            </template>
            <el-menu-item :index="'/'+cItem.path" v-for="cItem in item.children" :key="cItem.id" @click="saveNavPath('/'+cItem.path)">
              <template slot="title">
                <i :class="iconList[cItem.id]" class="icon"></i>
                <span>{{cItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>

export default {
  data() {
    return {
      menuList: [],
      iconList: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao',
        110: 'el-icon-user-solid',
        111: 'el-icon-s-custom',
        112: 'el-icon-unlock',
        104: 'el-icon-s-operation',
        115: 'el-icon-s-data',
        121: 'el-icon-menu',
        107: 'el-icon-s-order',
        146: 'el-icon-s-marketing'
      },
      isOpen: false,
      activePath: ''
    }
  },
  methods: {
    logout () {
      sessionStorage.removeItem('token')
      this.$router.push('/login')
    },
    async getMenus() {
      const { data: menuList } = await this.$http.get('menus')
      if (menuList.meta.status !== 200) return this.$message({ type: 'error', message: menuList.meta.msg })
      this.menuList = menuList.data
    },
    openAndClose() {
      this.isOpen = !this.isOpen
    },
    saveNavPath(activePath) {
      sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  },
  created() {
    this.getMenus()
    this.activePath = sessionStorage.getItem('activePath')
  }

}
</script>

<style lang="less" scoped="scoped">
  .home-container {
    height: 100%;
  }
  .el-header {
    background-color: #373d41;
    height: 80px!important;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .header-left{
      display: flex;
      align-items: center;
      img{
        height: 40px;
        width: 40px;
      }
      h2{
        display: inline-block;
        padding-left: 20px;
        color: #fff;
      }
    }
  }

  .el-aside {
    background-color: #333744;
    .openAndClose{
      height: 24px;
      background-color: #4a5064;
      line-height: 24px;
      text-align: center;
      letter-spacing: 0.2em;
      color: #fff;
      cursor: pointer;
    }
    .el-menu {
      border-right: none;
    }
  }
  .el-main {
    background-color: #eaedf1;
  }
  .icon {
    margin-right: 20px;
  }
</style>
